package UserInput;

import idp.MainController;

import java.util.Observable;

import debuggen.Debug;
import net.java.games.input.Component;
import net.java.games.input.Controller;

/**
 * Joystick
 * A better name for this class would be: controller.
 * But that name would let people think its something different.
 * @author Danny
 * Abstract class for joystick
 */
public abstract class Joystick extends Observable implements UserInput,
															 Runnable {
	// The joystick
	protected Controller cJoystick;
	// Buttons from joystick
	private Component[] cmpButtons;
	// Reference to main controller
	protected MainController mController;
	// The thread
	protected Thread t;
	
	/**
	 * Constructor
	 * @param p_cJoystick
	 */
	public Joystick(Controller p_cJoystick, MainController p_mcController)
	{
		// Place joystick in var
		cJoystick = p_cJoystick;
		
		// Config
		config();

		// Get instance of main controller
		mController = p_mcController;
		
		// Write some info to log if in debug mode
		if(Debug.DEBUGGEN)
		{
			Debug.writeMessage("Joystick: " + cJoystick.getName());
		}
	}
	/**
	 * config
	 * Some config for the joystick
	 */
	protected void config()
	{
		// Buttons
		cmpButtons = cJoystick.getComponents();
		// Create thread
		t = new Thread(this);
	}
	/**
	 * getJoystick
	 * @return Controller - Joystick
	 */
	public Controller getJoystick()
	{
		return cJoystick;
	}
	/**
	 * getJoystickName
	 * @return String - name of joystick
	 */
	public String getJoystickName()
	{
		return cJoystick.getName();
	}
	/**
	 * getComponents
	 * get the components from the Joystick
	 * @return
	 */
	public Component[] getComponents()
	{
		// Data must be synchronized
		synchronized(this)
		{
			return cmpButtons;
		}
	}
	/**
	 * Start
	 * Start thread
	 */
	public void start()
	{
		t.start();
	}
	/**
	 * setComponents
	 * Set new components containing values
	 * @param p_cmpNew - Components with new values
	 */
	public void setComponents(Component[] p_cmpNew)
	{
		synchronized(this)
		{
			cmpButtons = null;
			cmpButtons = p_cmpNew;
		}
	}
}
